<template>
	<div>
<!--		<el-button type="primary" @click="showAddDeviceDialog">新建工装</el-button>-->
		<el-table :data="baseData" style="width: 100%" height="calc(100vh - 230px)">
			<el-table-column prop="toolingId
" label="工装类型">
				<template #default="scope">
                    <span>{{
												data.deviceOptionsTool.find(
													(i) => i.id === scope.row.toolingId
												)?.toolingCode
											}}</span>
				</template>
			</el-table-column>
<!--			<el-table-column label="操作">-->
<!--				<template #default="scope">-->
<!--					<el-button type="text" @click="editDevice(scope.row)">编辑</el-button>-->
<!--					<el-button type="text" @click="deleteDevice(scope.row)">删除</el-button>-->
<!--				</template>-->
<!--			</el-table-column>-->
		</el-table>

		<!-- 新建夹具弹窗 -->
		<el-dialog title="新建工装" v-model="data.showDeviceDialog" width="500">
			<el-select v-model="data.newDevice.toolingId" placeholder="请选择工装" filterable style="width:98%">
				<el-option v-for="device in data.deviceOptionsTool" :key="device.id" :label="device.toolingCode"
									 :value="device.id" >
					<span style="float: left">{{ device.toolingCode }}</span>
					<span
						style="
          float: right;
          color: var(--el-text-color-secondary);
          font-size: 13px;
        "
					>
        {{ device.name }}
      </span>
				</el-option>
			</el-select>
			<template #footer>
				<el-button @click="data.showDeviceDialog = false">取消</el-button>
				<el-button type="primary" @click="addDevice">确定</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script setup>
import axiosInstance from '@/utils/http/api';
import { onMounted, reactive, watch, ref } from 'vue';
import { ElMessageBox } from 'element-plus';


const data = reactive({
	showDeviceDialog: false,
	newDevice: {
		toolingId: ''
	},
	deviceOptionsTool: []
});
const baseData = ref([]);
const indexParenId = ref();

const props = defineProps({
	devicesData: Array,
	onSubmit: Function
});

watch(props, (val) => {
	console.log(val, 'valvalval');
	if (val.devicesData) {
		baseData.value = val.devicesData.tool || []
		;
		indexParenId.value = val.devicesData.index;
		console.log(val, 'devicesData', baseData?.value);
	}
});

const showAddDeviceDialog = () => {
	data.showDeviceDialog = true;
	data.newDevice = {};
};

const getDeviceOptions = async () => {
	try {
		const response = await axiosInstance.post('/api/basedataTooling/getAll', {});
		console.log(response.data);
		data.deviceOptionsTool = response.data;
	} catch (error) {

	}
};
const replaceItemById = (items, newItem) => {
	const newData = items.map(i => {
		if (i.index_id === newItem.index_id) {
			return newItem;
		}
		return i;
	});
	return newData;

};

const addDevice = () => {
	console.log(data.newDevice, 'data.newDevice', baseData?.value);
	let addData = [];
	if (data.newDevice.index_id) {
		addData = replaceItemById(baseData?.value, data.newDevice);
	} else {
		const ids = Math.floor(Math.random() * 100);
		if (baseData.value) {
			addData = [...baseData.value, { ...data.newDevice, index_id: ids }];
		} else {
			addData = [{ ...data.newDevice, index_id: ids }];
		}
	}


	if (props.onSubmit) {
		props.onSubmit(addData, 'tool', indexParenId.value);
	}
	// data.newDevice.tool = '';
	data.showDeviceDialog = false;
};

const editDevice = (row) => {
	console.log(row);
	data.newDevice = row;
	data.showDeviceDialog = true;
};

const deleteDevice = (row) => {
	ElMessageBox.confirm('确定删除该条记录吗？', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	}).then(async () => {
		const delBaseData = baseData?.value.filter(i => i.index_id !== row.index_id);
		console.log(delBaseData, 'delBaseData ');
		if (props.onSubmit) {
			props.onSubmit(delBaseData, 'tool', indexParenId.value);
		}
	}).catch(() => {
	});

};

onMounted(() => {
	getDeviceOptions();
});
</script>

<style scoped>
.el-table {
	margin-top: 20px;
}
</style>
